const Koa        = require('koa');
const bodyparser = require('koa-bodyparser');
const view       = require('koa-views');
const koaError   = require('koa-onerror');
const convert    = require('koa-convert');
const koaStatic  = require('koa-static');
const logger     = require('koa-logger');
const http       = require('http');
const fs         = require('fs');

const app        = new Koa();
const api        = require('./routers/api.js');

app.convert = x => app.use.call(app, convert(x));

app.convert(bodyparser());
//logger
app.convert(logger());

//static
app.convert(koaStatic(__dirname+'/public'));

//发生默认err.ejs
koaError(app,{template: 'views/err.ejs'});
//router
app.use(api.routes(),api.allowedMethods());

// error logger
app.on('error',(err, ctx) => {
  console.log('error occured:', err)
});

http.createServer(app.callback()).listen(3000,()=>{
  console.log("http://127.0.0.1:3000 is runing");
});
